package com.xiaoma.tpo.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoma.tpo.dao.ForumPostDao;
import com.xiaoma.tpo.dao.ForumTopicDao;
import com.xiaoma.tpo.dao.ForumTopicStoreDao;
import com.xiaoma.tpo.dao.ForumUserWatchDao;
import com.xiaoma.tpo.domain.ForumPost;
import com.xiaoma.tpo.domain.ForumTopic;
import com.xiaoma.tpo.domain.ForumTopicStore;
import com.xiaoma.tpo.domain.ForumUserWatch;
import com.xiaoma.tpo.dto.ForumPostRaw;
import com.xiaoma.tpo.dto.ForumPostVo;
import com.xiaoma.tpo.dto.ForumTopicVo;
import com.xiaoma.tpo.dto.TopicSum;
import com.xiaoma.tpo.utils.Pager;

@Service
public class ForumService {

    @Autowired
    private ForumTopicDao topicDao;
    
    @Autowired
    private ForumPostDao postDao;
    
    @Autowired
    private ForumTopicStoreDao topicStoreDao;
    
    @Autowired
    private ForumUserWatchDao userWatchDao;
    
    public int saveForumTopic(ForumTopic topic) {
        return topicDao.insert(topic);
    }
    
    public int updateForumTopic(ForumTopic topic) {
        return topicDao.update(topic);
    }
    
    public ForumTopic getTopicById(int topicId) {
        return topicDao.getById(topicId);
    }
    
    public Pager findTopics(int startIndex, int itemsPerPage) {
        return topicDao.findTopics(startIndex, itemsPerPage);
    }
    
    public Pager findTopics(int startIndex, int itemsPerPage, int type) {
        return topicDao.findTopics(startIndex, itemsPerPage, type);
    }
    
    public List<ForumTopicVo> findOldTopics(int topicId, int itemsPerPage) {
        return topicDao.findOldTopics(topicId, itemsPerPage);
    }
    
    public List<ForumTopicVo> findOldTopics(int topicId, int itemsPerPage, int type) {
        return topicDao.findOldTopics(topicId, itemsPerPage, type);
    }
    
    public int getTopicCount(ForumTopic entity) {
        return topicDao.queryCount(entity);
    }
    
    public Pager findPostsByTopicId(Integer topicId, Integer startIndex, Integer itemsPerPage) {
        Pager pager1 = postDao.find1Posts(topicId, startIndex, itemsPerPage);
        List<ForumPostVo> postVos = new ArrayList<ForumPostVo>(pager1.getList().size());
        List<ForumPostRaw> list1 = pager1.getList();
        ForumPostRaw rawData = null;
        for (int i = 0; i < list1.size(); i++) {
            rawData = list1.get(i);
            ForumPostVo vo = new ForumPostVo();
            vo.setId(rawData.getId());
            vo.setUserName(rawData.getUserName());
            vo.setImgs(rawData.getImgs());
            vo.setContent(rawData.getContent());
            vo.setAvatar(rawData.getAvatar());
            vo.setCreatedAt(rawData.getCreatedAt());
            vo.setOwner(rawData.getOwner());
            vo.setTopicId(rawData.getTopicId());
            Pager p2 = postDao.find2Posts(vo.getId(), 0, 3);
            List<ForumPostRaw> list2 = p2.getList();
            vo.setReplyTotalCount(p2.getTotalCount());
            vo.setReplies(list2);
            postVos.add(vo);
        }
        pager1.setList(postVos);
        return pager1;
    }
    
    public Pager findPostChildren(int postId, int startIndex, int itemsPerPage) {
        return postDao.find2Posts(postId, startIndex, itemsPerPage);
    }
    
    public int savePost(ForumPost post) {
        return postDao.insert(post);
    }
    
    public void deleteTopic(int topicId) {
        topicDao.deleteTopic(topicId);
    }
    
    
    public void toTop(int topicId) {
        ForumTopic entity = new ForumTopic();
        entity.setId(topicId);
        entity.setTop(true);
        entity.setUpdatedAt(new Date());
        topicDao.update(entity);
        topicDao.limitTopNum(3);//置顶3条记录
    }
    
    public void saveStore(ForumTopicStore forumTopicStore) {
        topicStoreDao.insert(forumTopicStore);
    }
    
    public int deleteStore(int userId, int topicId) {
        return topicStoreDao.deleteStore(userId, topicId);
    }
    
    public List<ForumTopicVo> findStoredTopics(int userId) {
        return topicStoreDao.findStoredTopics(userId);
    }
    
    public ForumTopicStore findTopicStore(int userId, int topicId) {
        ForumTopicStore entity = new ForumTopicStore();
        entity.setTopicId(topicId);
        entity.setUserId(userId);
        List<ForumTopicStore> items = topicStoreDao.findByColumns(entity);
        return items.size() > 0 ? items.get(0) : null;
    }
    
    public void saveWatch(ForumUserWatch userWatch) {
        userWatchDao.insert(userWatch);
    }
    
    public ForumUserWatch findUserWatch(int userId, int teacherId) {
        ForumUserWatch entity = new ForumUserWatch();
        entity.setUserId(userId);
        entity.setTeacherId(teacherId);
        List<ForumUserWatch> items = userWatchDao.findByColumns(entity);
        return items.size() > 0 ? items.get(0) : null;
    }
    
    public int watchCount(int teacherId) {
        ForumUserWatch entity = new ForumUserWatch();
        entity.setTeacherId(teacherId);
        return userWatchDao.queryCount(entity);
    }
    
    public TopicSum topicSum(int userId) {
        return topicDao.topicSum(userId);
    }
    
    public List<ForumTopic> findTopics(int userId) {
        return topicDao.findByUserId(userId);
    }
    
    public List<ForumTopic> findFeedbackTopics(int userId) {
//        List<ForumTopic> list1 = topicDao.findResponsedTopics(userId);
        List<ForumTopic> list = topicDao.findJoinedTopics(userId);
//        list1.addAll(list2);
        return list;
    }
}
